<template>
	<view class="wrapper">
		<!-- 导航轮播图 -->
		<swiper class="swiper" indicator-dots autoplay circular indicator-color="#ccc" indicator-active-color="#fff">
			<block v-for="(item, index) in goods.pics" :key="index">
				<swiper-item>
					<image class="swiper-img" :src="item.pics_big" @click="prevImg(index)"></image>
				</swiper-item>
			</block>
		</swiper>
		<!-- 商品信息 -->
		<view class="goods-info">
			<view class="price">￥{{ goods.goods_price }}</view>
			<view class="name-favo">
				<view class="name">{{ goods.goods_name }}</view>
				<view class="favo">
					<button open-type="share"></button>
					<text class="iconfont icon-favor"></text>
					<text>收藏</text>
				</view>
			</view>
			<view class="express">快递: 免运费</view>
		</view>
		<!-- 促销部分 -->
		<view class="promote">
			<view>
				<text class="name">促销</text>
				<text class="detail">满300减30元</text>
			</view>
			<view class="brother">
				<text class="name">已选</text>
				<text class="detail gray">黑色/S/1件</text>
			</view>
		</view>
		<!-- 图文介绍 -->
		<view class="goods-detail">
			<view class="tabs">
				<text :class="{active:activeIndex === index}" v-for="(item, index) in menuList" @click="selectMenu(index)" :key="index">{{ item }}</text>
			</view>
			<view class="main">
				<rich-text v-if="!activeIndex" :nodes="goods.goods_introduce"></rich-text>
				<view v-else>商品参数</view>
			</view>
		</view>
		<!-- 底部购物车 -->
		<view class="fixed-bottom">
			<view class="icon-text">
				<text class="iconfont icon-kefu"></text>
				<text>联系客服</text>
			</view>
			<view class="icon-text" @click="goCart">
				<text class="iconfont icon-cart-full"></text>
				<text>购物车</text>
			</view>
			<view class="btn add-cart-btn" @click="addCart">加入购物车</view>
			<view class="btn buy-btn" @click="toPay">立即购买</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goods: {},
				menuList: ['图文介绍', '规格参数'],
				activeIndex: 0
			}
		},
		onLoad(options) {
			// console.log(options.goodsid)
			this.goodsid = options.goodsid
			this.getGoodsDetail()
		},

		//  设置分享弹窗
		onShareAppMessage() {
			return {
				title: this.goods.goods_name,
				imageUrl: this.goods.pics[0].pics_big
			}
		},
		methods: {
			// 1、获取商品详情。
			async getGoodsDetail() {
				this.goods = await this.$request({
					url: '/api/public/v1/goods/detail?goods_id=' + this.goodsid
				})
				// console.log(this.goods)
			},

			// 2、点击预览放大图片
			prevImg(index) {
				let imgs = this.goods.pics.map(item => {
					// 返回 数组pics.big
					return item.pics_big
				})
				// 调用api方法：wx.previewImage()
				uni.previewImage({
					urls: imgs,
					current: imgs[index]
				})
			},

			// 3、设置tab栏点击事件
			selectMenu(index) {
				this.activeIndex = index
			},
			// 4、跳转至购物车
			goCart() {
				uni.switchTab({
					url: '/pages/cart/cart'
				})
			},
			
			// 5、添加至购物车
			// addCart() {
				// 对象方式：
				// 方法： 取出 更新 存储
				//1、点击立即获取本地数据
				// let cartData = uni.getStorageSync('cart') || {}
				//2、添加后更新本地数据
				//   获取数据id名
				// let goodsId = this.goods.goods_id
				// console.log(goodsId)
				//   判断是否 是首次添加  num ? 1 : num++
				// cartData[goodsId] = {
				// 	num: cartData[goodsId] ? cartData[goodsId].num + 1 : 1,
				// 	checked: true
				// }
				//3、更新后进行存储
				// uni.setStorageSync('cartData',cartData)

				// ===============================================================================

				// 数组方式：
				// 方法： 取出 更新 存储
				addCart() {
				// 1、取出：
				let cartData = uni.getStorageSync('cartData') || [];
				// 2、更新：
				let goodsId = this.goods.goods_id
				console.log(goodsId)
				// cartData[goodsId] = {
				// num: cartData[goodsId] ? cartData[goodsId].num + 1 : 1,
				// checked: true,
				// };
				// 3、存储：
				// 查找 find：点击当前商品加入购物车后 需查找本地存储是否有相同
				let targetGoods = cartData.find(item => {
					return item.goodsId === goodsId
				})
				// console.log(targetGoods)
				// 判断取到的值 是否是首次添加
				if (targetGoods) {
					targetGoods.checked = true
					targetGoods.num = targetGoods.num + 1
					cartData = [...new Set([targetGoods, ...cartData])]
				} else {
					let firstGoods = {
						goodsId,
						num: 1,
						checked: true
					}
					// 加入至最前面
					cartData.unshift(firstGoods)
				}
				// 存储：
				uni.setStorageSync('cartData', cartData)
				
				// 存储完成跳转后提示
				uni.showToast({
					title:'添加购物车成功！'
				})
			},
			toPay(){
				// 直接点击进入支付页面 传递goodsId
				uni.navigateTo({
					url:'/pages/pay/pay?goodsId=' +this.goods.goods_id
				})
			}
		}
	}
</script>
<style lang="less">
	.wrapper {
		background-color: #f4f4f4;
		padding-bottom: 98rpx;
	}

	.swiper {
		height: 720rpx;
	}

	.swiper-img {
		width: 100%;
		height: 720rpx;
	}

	.goods-info {
		padding: 40rpx 0 30rpx 20rpx;
		background-color: #fff;

		.price {
			color: #eb4450;
			font-size: 40rpx;
		}

		.name-favo {
			display: flex;
			height: 78rpx;
			margin-top: 14rpx;

			.name {
				color: #333;
				flex: 1;
				font-size: 28rpx;
				padding-right: 78rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 2; // 控制多行的行数
				-webkit-box-orient: vertical;
			}

			.favo {
				border-left: 1px solid #ddd;
				width: 144rpx;
				color: #999;
				font-size: 24rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				position: relative;
			}
		}

		.express {
			color: #999;
			font-size: 24rpx;
			margin-top: 20rpx;
		}
	}

	.promote {
		margin-top: 20rpx;

		view {
			background-color: #fff;
			height: 96rpx;
			line-height: 96rpx;
			font-size: 28rpx;
			margin-top: 20rpx;
			padding-left: 20rpx;

			&.brother {
				margin-top: 0;
			}

			.name {
				color: #404040;
			}

			.detail {
				color: #ff4055;
				margin-left: 40rpx;

				&.gray {
					color: #dfdfdf;
				}
			}
		}
	}

	.goods-detail {
		margin-top: 20rpx;
		background-color: #fff;

		.tabs {
			display: flex;

			text {
				flex: 1;
				text-align: center;
				color: #404040;
				font-size: 22rpx;
				height: 100rpx;
				line-height: 100rpx;
				box-sizing: border-box;

				&.active {
					color: #ff4055;
					border-bottom: 10rpx solid #ff2644;
				}
			}
		}
	}

	.fixed-bottom {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		height: 98rpx;
		display: flex;
		background-color: #fff;

		.icon-text {
			flex: 2;
			font-size: 20rpx;
			color: #404040;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			position: relative;
		}

		.btn {
			flex: 3;
			color: #fff;
			text-align: center;
			line-height: 98rpx;

			&.add-cart-btn {
				background-color: #ffb400;
			}

			&.buy-btn {
				background-color: #ff2d4a;
			}
		}
	}
</style>
